package com.roger.uia.helper.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.roger.uia.helper.UiBase;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class PicUtils {
    private static final String TAG = "Test" + PicUtils.class.getSimpleName();

    public static PicUtils pic(){ return new PicUtils(); }

    /**
     * Screenshot
     * @param file     the target pic
     */
    @SuppressWarnings("ResultOfMethodCallIgnored")
    public void takeScreenshot(String file){
        File f = new File(file);
        if(!f.getParentFile().exists())  f.getParentFile().mkdirs();
        UiBase.mDevice.takeScreenshot(f);
    }
    
    /**
     * is the pic same
     * @param f1         the first pic
     * @param f2         the second pic
     * @param percent    different parts less than this percent will be false
     * @return           true or false
     */
    public boolean isPicSameAs(String f1, String f2, double percent){
        Bitmap bm1 = getBitMap(f1);
        Bitmap bm2 = getBitMap(f2);
        if(bm1 != null && bm2 != null){
            if (bm1.getHeight() != bm2.getHeight() || bm1.getWidth() != bm2.getWidth() || bm1.getConfig() != bm2.getConfig()) {
                return false;
            } else {
                int width = bm1.getWidth();
                int height = bm2.getHeight();
                int numDiffPixels = 0;
                for (int y = 0; y < height; y++) {
                    for (int x = 0; x < width; x++) {
                        if (bm1.getPixel(x, y) != bm2.getPixel(x, y)) {
                            numDiffPixels++;
                        }
                    }
                }
                double numberPixels = height * width;
                double diffPercent = numDiffPixels / numberPixels;
                return percent <= 1.0d - diffPercent;
            }
        }
         return false;
    }

    /**
     * is the pic part same
     * @param f1        the first pic
     * @param f2        the second pic
     * @param percent   different parts less than this percent will be false
     * @param startX    startY,endX,endY is the comparison range
     * @return          true or false
     */
    public boolean isPicSameAs(String f1, String f2, double percent,int startX,int startY,int endX,int endY){
        Bitmap bm1 = getBitMap(f1);
        Bitmap bm2 = getBitMap(f2);
        if(bm1 != null && bm2 != null){
            int width = bm2.getWidth();
            int height = bm2.getHeight();
            if(startX<0 || startX>=width || endX<0 || endX>=width || startX>endX || startY<0 || startY>=height || endY<0 || endY>=height || startY>endY){
                Log.d(TAG,"error with your comparison range");
                return false;
            }else{
                if (bm1.getHeight() != bm2.getHeight() || bm1.getWidth() != bm2.getWidth() || bm1.getConfig() != bm2.getConfig()) {
                    return false;
                } else {
                    int numDiffPixels = 0;
                    for (int y = startY; y <= endY; y++) {
                        for (int x = startX; x <= endX; x++) {
                            if (bm1.getPixel(x, y) != bm2.getPixel(x, y)) {
                                numDiffPixels++;
                            }
                        }
                    }
                    double numberPixels = (endX-startX) * (endY-startY);
                    double diffPercent = numDiffPixels / numberPixels;
                    return percent <= 1.0d - diffPercent;
                }
            }
        }
        return false;
    }

    public Boolean isPicBlack(String f){
        Bitmap bm = getBitMap(f);
        if(bm != null) {
            int width = bm.getWidth();
            int height = bm.getHeight();
            int numDiffPixels = 0;
            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    long m = bm.getPixel(x, y);
                    if(m == -16777216)
                        numDiffPixels++;
                }
            }
            double numberPixels = height * width;
            double diffPercent = numDiffPixels / numberPixels;
            return diffPercent > 0.995d;
        }
        return false;
    }

    public Boolean isPicWhite(String f){
        Bitmap bm = getBitMap(f);
        if(bm != null) {
            int width = bm.getWidth();
            int height = bm.getHeight();
            int numDiffPixels = 0;
            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    long m = bm.getPixel(x, y);
                    if(m == -1)
                        numDiffPixels++;
                }
            }
            double numberPixels = height * width;
            double diffPercent = numDiffPixels / numberPixels;
            return diffPercent > 0.995d;
        }
        return false;
    }

    /**
     * read pic
     * @param   file   the pic to be opened for reading
     * @return         bitmap
     */
    private Bitmap getBitMap(String file){
        Bitmap bm = null;
        try {
            File f = new File(file);
            FileInputStream fis = new FileInputStream(f);
            bm = BitmapFactory.decodeStream(fis);
            fis.close();
        } catch (IOException e) {
            Log.d(TAG,"get bitmap fail:" + file);
        }
        return bm;
    }

}
